package com.rams.jhms.gui.datamodels;

import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractListModel;
import javax.swing.ListModel;

import com.rams.jhms.dao.DerbySettingsFSFoldersDAO;
import com.rams.jhms.dto.FSPropsFolderDTO;

public class FSPropsFolderListModel extends AbstractListModel implements ListModel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 588805408200195169L;
	
	DerbySettingsFSFoldersDAO fsDAO = new DerbySettingsFSFoldersDAO();
	List<FSPropsFolderDTO> foldersList = new ArrayList<FSPropsFolderDTO>(); 
	
	public FSPropsFolderListModel() {
		foldersList = fsDAO.getFolders();
	}
	
	private void readFolders() {
		foldersList = fsDAO.getFolders();
		fireContentsChanged(this, 0, foldersList.size());
	}
	
	public int getSize() {
		return foldersList.size();
	}

	public Object getElementAt(int index) {
		return foldersList.get(index);
	}

	public void addElement(String path) {
		fsDAO.addFolder(path);
		readFolders();
	}
	
	public void removeElementAt(int index) {
		FSPropsFolderDTO f = foldersList.get(index);
		if (f != null)
			fsDAO.removeFolder(f.getId());
		readFolders();
	}
}
